import java.io.*;
import java.util.*;

public class Piste {
	private Boolean active;
	private MPlayer player;
    private String titre;
    private ArrayList<Canal> lesCanaux;
    private double periode;	// periode d'echantillonnage en microseconde
    private int frequence;
    private int resolution;	// nb d'octet pour un echantillon
    private boolean signe ; // les echantillons sont signés(true) ou non signés(false)


    int getFrequence(){
	return frequence;
    }

    double getPeriode(){
	return periode;
    }

    int getResolution(){
	return resolution;
    }

    int getNbCanaux(){
	return lesCanaux.size();
    }

    boolean getSigne(){
	return signe;
    }

    int getNbEch(){
	return lesCanaux.get(0).getNbEch() * lesCanaux.size();
    }

    Canal getCanal(int n){
	return lesCanaux.get(n);
    }

    String getTitre(){
	return titre;
    }

    void setTitre(String t){
	titre = t;
    }

    Piste(int nbcanaux, int frequence, int resolution,  long size){
    this.active = false;
    this.player = null;
	lesCanaux = new ArrayList<Canal>(nbcanaux);
	for (int i=0 ; i < nbcanaux ; i++)
	    lesCanaux.add(new Canal(size/nbcanaux));
	this.periode = 1000.0/frequence; // periode en microseconde
	this.resolution =resolution ;
	this.frequence = frequence;
	periode = 1000/frequence ; // 16khz
	signe = true;
	
    }

    Piste(){
    this.active = false;
    this.player = null;
	lesCanaux = new ArrayList<Canal>();
	lesCanaux.add(new Canal());
	this.frequence = 16000;
	periode = 1000/frequence ; // 16khz
	resolution = 2;
	signe = true;
    }


    static int creerEchantillon(byte[] buf,int offset, int resol){
	int i = resol-1;
	int res = buf[i + offset];
	resol--;
	while (resol > 0){
	    i--;
	    res = res * 256 + (buf[i + offset] &0xFF);
	    resol--;
	}
 	return res;
    }

    static final int PAQUET_ECH = 64000; // paquet d'echantillons pour optimiser les transferts

    public static Piste loadFromRawFile(String filename,int nbcanaux, int frequence, int resolution){
	Piste res = null;
	InputStream fis;
	int echantillonCour;
	int numCanalCour = 0;
	byte[] buffer;
	int n;
	try{
	    System.out.println("start loading ... ");
	    File finput = new File(filename);
	    fis = new BufferedInputStream(new FileInputStream(finput),1000000);
	    res = new Piste(nbcanaux, frequence, resolution, finput.length()/resolution);
	    buffer = new byte[resolution * PAQUET_ECH];
	    n = fis.read(buffer,0,resolution * PAQUET_ECH);
	    while (fis.available() > 0){
		int nech = n / resolution;
		for (int i = 0 ; i < nech ; i++){
		    echantillonCour = creerEchantillon(buffer, i * resolution, resolution);
		    res.lesCanaux.get(numCanalCour).addEchantillon(echantillonCour);
		    numCanalCour = (numCanalCour + 1) % nbcanaux;
		}
		n = fis.read(buffer,0,resolution * PAQUET_ECH);
	    }
	    
	    System.out.println("... end loading mini = " + res.lesCanaux.get(0).getMini() + " maxi = " + res.lesCanaux.get(0).getMaxi() );

	}catch(IOException e){System.out.println("--- end loading");}
	System.out.println("Canal 1 : " + res.lesCanaux.get(0).getNbEch());
	res.setTitre(filename.substring(0, filename.indexOf('.')));

	return res;
    }

    void initLecture(){
	for (Canal c : lesCanaux)
	    c.initLecture();
    }

    boolean finLecture(){
	boolean res = false;
	for (Canal c : lesCanaux)
	    res = res | c.finLecture();
	return res;
    }
    
    /*
     * Rend active la piste sur le player en parametre
     */
    public void addPlayer(MPlayer player) {
    	this.player = player;
    	active = true;
    }
    
    /*
     * Desactive la piste
     */
    public void retirerPlayer() {
    	this.player = null;
    	active = false;
    }
    
    /*
     * Getter
     */
    public MPlayer getPlayer() {
    	return player;
    }
    
    /*
     * Getter
     */
    public Boolean estActive() {
    	return active;
    }

}


